
$(document).ready(function(){
	var $right = $('.right'),
		$rightFixed = $right.children().first(),
		$rightHeight = $right.height(),
		$rightTop = $right.offset().top,
		$loadingContent = $('.loading-content'),	//底部loading...div
		$loadingBtn = $('.loading-btn'),	//底部loading加载出错显示的链接
		allowLoading = true,//是否允许加载数据，防止多次请求
		showLoading = true, //是否显示loading...
		isDown = true,	//是否需要判断往下滚
		oldCount = $('.bd-c-list').length,	//取得页面出事图书列表条书
		type = $('.hidden-type').val();
		keyword = $('.hidden-keyword').val();
		$loadedCount = $('.loaded-count');	//已加载条数
		timer = null,
		timer2 = null,
		$bdContent = $('.bd-content');
	//判断是否有图书
	if($('.bd-c-list').length==0){
		allowLoading = false ;
	}
	//瀑布流布局
	$bdContent.imagesLoaded(function() {
		$bdContent.masonry({
			itemSelector: '.bd-c-list',
			transitionDuration: 0, //动态效果过度事件，0为禁用动态效果
			gutter: 15
		});
	});
	//动态加载内容ajax
	var queryBookByLikeAjax = function(type,keyword,index,count){
		BC.ajax({
			type:'POST',
			url:'/bookcrossing/querybookbyajax',
			data:{type:type,keyword:keyword,index:index,count:count},
			b:function(){
				if(showLoading){$loadingContent.show();}	//显示loading...
				allowLoading = false;
			},
			s:function(resp){
				if(resp.bookData.length>0){
					var $temp = $(parseBookInfo(resp.bookData));
					$temp.css('display','none');
					$bdContent.append($temp);
					oldCount += resp.bookData.length; //更新页面图书列表条数
					$loadedCount.html(oldCount);
					$bdContent.imagesLoaded(function(){
						$temp.fadeIn(function(){
							allowLoading = true;
						});
						$bdContent.masonry('layoutItems',$bdContent.masonry('addItems', $temp),false);
					})
				}else{
					$loadingBtn.after('<p class="alert alert-warning no-more-content">没有更多内容了亲 ^_^</p>');
				}
			},
			f:function(resp){
				showLoading = false;
				$loadingBtn.show();
			},
			e:function(){
				showLoading = false;
				$loadingBtn.show();
			},
			c:function(){
				$loadingContent.hide();	//隐藏loading...
			}
		});
	}
	//绑定右边导航fixed定位及动画 和 下拉到底部动态加载内容
	$(document).scroll(function(){
		var scrollTop = $(document).scrollTop(),
			windowHeight = $(window).height(),
			docHeight = $(document).height();
		if(timer){
			clearTimeout(timer);
		}
		timer = setTimeout(function(){
			if(isDown){
				if(scrollTop >= $rightHeight+$rightTop-20){
					$rightFixed.addClass('right-fixed').css({top:20-$rightHeight});
					$rightFixed.animate(
						{top:60},
						400,
						'swing'
					);
					isDown = false;
				}
			}
			else{
				if(scrollTop<=55){
					$right.css({top:0});
					$rightFixed.removeClass('right-fixed').css({top:0});
					isDown = true;
				}
			}
		},10);
		//下拉到底部触发事件
		if(docHeight-windowHeight-scrollTop<80 && allowLoading){
			if(timer2){window.clearTimeout(timer2);}
			timer2 = window.setTimeout(function(){
				queryBookByLikeAjax(type,keyword,oldCount+1,5);
			},50)
		}
	});
	//绑定动态加载出错后的按钮事件
	$loadingBtn.on('click',document,function(){
		showLoading = true ;
		$loadingBtn.hide();
		queryBookByLikeAjax(type,keyword,oldCount+1,2);
	});
	//绑定返回顶部按钮
	BC.returnTop('#toTop',300,100);
	
	//判断是否有cookie记住账号密码
	var $hiddenSession = $('.hidden-session');
	if($hiddenSession.length==0){
		if(BC.CookieUtil.get('uid')!=null && BC.CookieUtil.get('password')!=null){
			var uid = BC.CookieUtil.get('uid'),
				password = BC.CookieUtil.get('password');
			BC.ajax({
				type:'POST',
				url:'/bookcrossing/login',
				dataType:'json',
				data:{username:uid,password:password},
				s:function(resp){
					$('.navbar-right').html(parseCookieLogin(resp));
					//window.location.reload();
				}
			});
		}
	}
	//处理拼装后台传回的图书数据
	function parseBookInfo(data){
		var str1 = '';
		for(var i=0; i<data.length; i++){
			var bId = data[i]['bid'],
				picUrl = data[i]['picurl'],
				bName = data[i]['bname'],
				author = data[i]['author'],
				introduce = data[i]['other'],
				str2 = '<div class="bd-c-list">';
			if(BC.getByteLen(introduce)>190){
				introduce = BC.getByteVal(introduce,190)+'...';
			}
			str2 += '<div class="book-pic"><a href="/bookcrossing/book?bid='+bId+'">';
			str2 += '<img src="/bookcrossing/img/book/'+picUrl+'" alt="'+bName+'" /></a></div>';
			str2 += '<p class="book-title"><a href="/bookcrossing/book?uid='+bId+'" title="'+bName+'">'+bName+'</a></p>';
			str2 += '<p class="book-author">'+author+'</p>';
			str2 += '<p class="book-introduce">'+introduce+'</p>';
			str2 += '</div>';
			str1 += str2 ;
		}
		return str1;
	}
	//处理cookie登陆成功返回uid和uname拼装内容
	function parseCookieLogin(resp){
		var str  = '',
			showName = '';
			upic = resp.upic;
		if(resp.uname!='null'){
			showName = resp.uname;
		}else{
			showName = resp.uid;
		}
		if(BC.getByteLen(showName)>8){
			showName = BC.getByteVal(showName,8)+'...';
		}
		str += '<li><a href="" title="'+showName+'"><span class="user-show"><img src="/bookcrossing/img/user/'+upic+'"/></span>'+showName+'</a></li>';
		str += '<li><a href="book/add" title="发布新书"><span class="glyphicon glyphicon-plus"></span></a></li>';
		str += '<li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">';
		str += '<span class="glyphicon glyphicon-envelope"></span> <span class="caret"></span></a>';
		str += '<ul class="dropdown-menu"><li><a href="#">通知</a></li><li><a href="#">私信</a></li></ul></li>';
		str += '<li><a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-cog"></span>';
		str += ' <span class="caret"></span></a><ul class="dropdown-menu"><li><a href="/bookcrossing/user/info">修改个人资料</a></li>';
		str += '<li><a href="logout" class="logout">退出</a></li></ul></li>';
		return str ;
	}
})